package utils

import (
	"image/color"

	"github.com/mojocn/base64Captcha"
)

// 10240个，和两分钟缓存
var store = base64Captcha.NewMemoryStore(base64Captcha.GCLimitNumber, base64Captcha.Expiration/5)

// 生成验证码
func CreateImageCode() (id, b64s string, err error) {
	var driver base64Captcha.Driver
	var driverString base64Captcha.DriverString

	// 配置验证码信息
	captchaConfig := base64Captcha.DriverString{
		Height:          60,
		Width:           200,
		NoiseCount:      1 | 3,
		ShowLineOptions: 2 | 4,
		Length:          6,
		// 难以辨认的字母都改为大写
		Source: "1234567890abdefghjmnqrtuyABCDEFGHJKLMNOPQRSTUVWXYZ",
		BgColor: &color.RGBA{
			R: 211,
			G: 239,
			B: 255,
			A: 1,
		},
		Fonts: []string{"wqy-microhei.ttc"},
	}

	driverString = captchaConfig
	driver = driverString.ConvertFonts()
	captcha := base64Captcha.NewCaptcha(driver, store)
	lid, lb64s, lerr := captcha.Generate()
	return lid, lb64s, lerr
}

// 验证验证码
func CaptVerify(id string, val string) bool {
	if id == "" || val == "" {
		return false
	}
	// 同时在内存清理掉这个图片
	return store.Verify(id, val, true)
}
